<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title>创建问题</title>
		<link rel="stylesheet" type="text/css" href="../../css/api.css"/>
		<link rel="stylesheet" href="../../css/weui.min.css" />
		<link rel="stylesheet" href="../../css/mui.min.css" />
		<link rel="stylesheet" href="../../css/base.css"/>
		<style type="text/css" media="screen">
			.mui-input-group .mui-input-row {
				height: 40px;
			}
			.head_div {
				background: #F0F0F0;
				height: 30px;
			}
			.head_div span {
				padding: 5px 10px;
			}
			input[type=color], input[type=date], input[type=datetime-local], input[type=datetime], input[type=email], input[type=month], input[type=number], input[type=password], input[type=search], input[type=tel], input[type=text], input[type=time], input[type=url], input[type=week], select, textarea {
				line-height: 30px;
				width: 100%;
			}
			.stuname {
				padding: 11px 0;
				line-height: 2.1;
			}
			.padding_left_10 {
				padding-left: 10px;
			}
			.padding_right_10 {
				padding-right: 10px;
			}
			.bottom_15 {
				padding-bottom: 15px;
			}
			.bottom_0_position {
				position: absolute;
				bottom: 5px;
				right: 10px;
			}
			.height50 {
				height: 50px;
				line-height: 50px;
			}
			.add_pic_span {
				text-align: center;
				width: 100%;
			}
			.img_pic_style {
				width: 100%;
			}
			.img-del {
				position: absolute;
				right: 5px;
				width: 20px;
				top: 400px;
			}
			.span_wight {
				width: 86%;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		</style>
	</head>
	<body>
		<div class="base_style_color base_title_Immerse" id='BarAppearance' ></div>
		<div class="base_navigation_bar base_style_color base_navigation_item_bar base_title_position" >
			<a class="base_left_item  nav-item" href="#" onclick="backClick()" style="color: #fff;" > 取消 </a>
			<span id="newsTitle">创建提问</span>
			<a class="base_right_item nav-item" id="wancheng"onclick="loaddata()"> 完成 </a>
		</div>
		<div class=" base_content_div base_content_font"  >
			<form class="  mui-input-group">
				<div class="head_div" >
					<span class="base_content_font base_dark_grey" >提问名称</span>
				</div>
				<div class="mui-input-row" >
					<input id="name" class="mui-navigate-right base_title_font base_black_grey  " maxlength="20" type="text" placeholder="" style="padding-left: 10px;padding-right: 10px;" maxlength="30" onkeyup="chagetext20()" onkeydown="return onKeyClick()">
				</div>
				<!--<div class="head_div" >
				<span class="base_content_font base_dark_grey"  >选择班级</span>
				</div>-->
				<!--	<div class="mui-input-row" onclick="openclass()">
				<span class="mui-navigate-right span_wight  base_title_font base_black_grey " style="padding-left: 10px;line-height: 2.1;" id="select_class"  onclick="openclass()"></span>
				</div>-->
				<div class="head_div" >
					<span class="base_content_font base_dark_grey" >提问内容</span>
				</div>
				<div class="mui-input-row" style="height: 180px" >
					<textarea id="content" class="textarea_t padding10 base_title_font base_black_grey  " maxlength="300" placeholder='描写本次提问内容，最多输入300个字，可不填写' onkeyup="chagetext()" style="height: 150px;"></textarea>
					<samp class="bottom_0_position base_content_font base_black_grey">还可以输入<em id="text_num">0</em>/300字</samp>
				</div>
				<div class="head_div height50" >
					<em class="base_title_font  base_black_grey add_pic_span" >+ 添加照片</em>
				</div>
				<div style="text-align: center;margin-left: 10px; margin-right: 10px;display:none; "   id="img_div">
					<img class="img-del" src="../../image/icon_img_del.png" onclick="delpic()"/>
					<img src="../../image/course_placehoder.png"  class="img_pic_style top_10  " id="img_pic" />
				</div>
			</form>
		</div>
		<div id="picture" class="mui-popover mui-popover-action mui-popover-bottom">
			<ul class="mui-table-view">
				<li class="mui-table-view-cell">
					<a href="#" typeid="0" style="color:  #222222">拍照</a>
				</li>
				<li class="mui-table-view-cell">
					<a href="#" typeid="1" style="color:#222222">从相册选取</a>
				</li>
			</ul>
			<ul class="mui-table-view">
				<li class="mui-table-view-cell">
					<a href="#picture" typeid="2" style="color:#222222"><b>取消</b></a>
				</li>
			</ul>
		</div>
		<div id="offlineAlert" class="weui_dialog_confirm" style="display: none;">
			<div class="weui_mask"></div>
			<div class="weui_dialog">
				<div class="weui_dialog_hd">
					<strong class="weui_dialog_title">是否确定退出当前页面？</strong>
				</div>
				<div class="weui_dialog_bd"></div>
				<div class="weui_dialog_ft">
					<a onclick="cleanCancel()" href="#" class="weui_btn_dialog default">取消</a>
					<a onclick="cleanConfirm()" href="#" class="weui_btn_dialog primary"  style="color:#18b4ed">确定</a>
				</div>
			</div>
		</div>
		<div id="offlineASkAlert" class="weui_dialog_confirm" style="display: none;">
			<div class="weui_mask"></div>
			<div class="weui_dialog">
				<div class="weui_dialog_hd">
					<strong class="weui_dialog_title">是否确定开启此提问？</strong>
				</div>
				<div class="weui_dialog_bd"></div>
				<div class="weui_dialog_ft">
					<a onclick="cleanConfirm()" href="#" class="weui_btn_dialog default">取消</a>
					<a onclick="actionDosign()" href="#" class="weui_btn_dialog primary"  style="color:#18b4ed">确定</a>
				</div>
			</div>
		</div>
	</body>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/jquery.min.js"></script>
	<script type="text/javascript" src="../../script/mui.min.js"></script>
	<script type="text/javascript" src="../../script/common.js"></script>
	<script type="text/javascript" src="../../script/MD5.js"></script>
	<script type="text/javascript" src="../../script/processing_images.js"></script>
	<script type="text/javascript" src="../../script/timeStampTool.js"></script>
	<script type="text/javascript" src="../../script/requestTool.js"></script>
	<script type="text/javascript">
		var path = '';
		//图片了路径
		var id = '';
		//问答id
		var classarr = [];
		var classid = '';
		//1添加  2：编辑
		var type = 1;
		var name = '';
		var nameInitVal = '';//初始名称
		var contentInitVal = '';//初始内容
		var imgPathInitVal = '';//初始图片地址
		apiready = function() {
			pageParaData();
			miuilist();
			keyback();
		};
		function pageParaData() {
			var pageparam = api.pageParam;
			type = api.pageParam.type;
			//1添加  2：编辑
			if (type == 2) {
				$("#newsTitle").html("编辑提问");
				$("#wancheng").html("保存");
				id = pageparam.id;
				getdata();
			} else {
				getZcode();
			}
			//			//获取班级数组
			//			api.addEventListener({
			//				name : 'refreshClassData'
			//			}, function(ret, err) {
			//				//coding...
			//				if (ret.value) {
			//					classarr = ret.value.data;
			//					className = '';
			//					classid = '';
			//					for (var i = 0; i < classarr.length; i++) {
			//						className = className + classarr[i].name + ",";
			//						classid = classid + classarr[i].id + ",";
			//					}
			//					className = className.slice(0, className.length - 1);
			//					classid = classid.slice(0, classid.length - 1);
			//					$("#select_class").text(className);
			//				}
			//			});
			if (type == 2) {//编辑提问
				sendBuriedNotifi('0', '老师端提问编辑页', '202')
			} else {//创建提问
				sendBuriedNotifi('0', '老师端提问创建页', '203')
			}
		}
		function onKeyClick() {
	        if (event.keyCode == 13) {
	            return false;
	        }
	    }
		function sendBuriedNotifi(busionessid, pagetype, pagetypeid) {
			var userinfo = {
				uid : $api.getStorage('cfnetppuid'),
				jid : $api.getStorage('cfnetppjxid'),
				uname : $api.getStorage('cfnetppusername'),
			}
			var data = {
				busionessid : busionessid,
				pagetype : pagetype,
				pagetypeid : pagetypeid,
				sourceurl : api.pageParam.sourceurl,
				sourcetitle : api.pageParam.sourcetitle,
				accesspageurl : window.location.pathname,
				accesspagetitle : document.title,
				userinfo : userinfo
			}
			SendBehaviorTrajectoriesNotifi_Study(data);
		}

		//选择图片
		function miuilist() {
			$('.add_pic_span').click(function() {
				mui('#picture').popover('toggle');
			});
			mui('body').on('tap', '.mui-popover-action li>a', function() {
				var typeid = $(this).attr('typeid');
				if (typeid == 0) {//拍照
					mui('#picture').popover('toggle');
					api.getPicture({
						sourceType : 'camera',
						encodingType : 'jpg',
						mediaValue : 'pic',
						destinationType : 'url',
						allowEdit : true,
						quality : 50,
						saveToPhotoAlbum : false
					}, function(ret, err) {
						if (ret) {
							var photoPath = ret.data;
							if (photoPath != '') {
								$('#img_pic').attr('src', photoPath);
								$('#img_div').show();
								addpatrol(photoPath);
							}
						} else {
							api.toast({
								msg : JSON.stringify(err)
							});
						}
					});
				} else if (typeid == 1) {//从相册选择
					mui('#picture').popover('toggle');
					api.getPicture({
						sourceType : 'album',
						encodingType : 'jpg',
						mediaValue : 'pic',
						destinationType : 'url',
						allowEdit : true,
						quality : 50,
						saveToPhotoAlbum : false
					}, function(ret, err) {
						if (ret) {
							var photoPath = ret.data;
							if (photoPath != '') {
								$('#img_pic').attr('src', photoPath);
								$('#img_div').show();
								addpatrol(photoPath);
							}
						} else {
						}
					});
				}
			});
		}

		//上传图片
		function addpatrol(compressImg) {
			showProgress();
			UploadFile_ali(Upload_File_img, compressImg, function(ret) {
				path = ret.fileurl;
				//				alert(JSON.stringify(ret));
				hideProgress();
			});
		}

		//删除图片
		function delpic() {
			path = '';
			$('#img_div').hide();
			$('#img_pic').attr('src', '');
		}

		//返回
		function backClick() {
			var cuName = $('#name').val();
			var cuContent = $('#content').val();
			// console.log('nameInitVal:'+nameInitVal+'\ncuName:'+cuName+'\ncontentInitVal:'+contentInitVal+'\ncuContent:'+cuContent+'\nimgPathInitVal:'+imgPathInitVal+'\npath:'+path);
			if (nameInitVal == cuName && contentInitVal == cuContent && imgPathInitVal == path) {
				cleanConfirm();
			}else{
				$('#offlineAlert').show();
			}
		}

		//取消
		function cleanCancel() {
			$('#offlineAlert').hide();
		}

		//确定
		function cleanConfirm() {
			if (type == 2) {//编辑提问
				sendBuriedNotifi('1', '老师端提问编辑页', '202')
			} else {//创建提问
				sendBuriedNotifi('1', '老师端提问创建页', '203')
			}
			api.closeWin({
			});
		}

		//提问名称字数不能超过20字
		function chagetext20() {
			//		alert($api.trimAll($('#name').val()));
			var strname = $api.trimAll($('#name').val());
			var len = strname.length;
			//		alert(len);
			if (len == 20) {
				ShowToast("提问名称最长20字");
			}
		}

		//监听剩余字数
		function chagetext() {
			var num = 300 - $('#content').val().length;
			$('#text_num').html(num);
		}

		//获取编码
		function getZcode() {
			api.showProgress();
			var params = {
				jid : $api.getStorage('cfnetppjxid'),
			}
			cfnetppPOST(url_getZcode, params, true, function(data, status) {
				api.hideProgress();
				//				 alert(JSON.stringify(data));
				if (status == 'success') {
					if (data.code == 200) {
						var zcode = data.zcode;
						$('#name').val("提问-" + zcode);
					} else {
						$('#name').val("提问-" + getNowDateYue());
					}
				} else {
					$('#name').val("提问-" + getNowDateYue());
				}
				nameInitVal = $('#name').val();
			})
		}

		//获取互动数据
		function getdata() {
			api.showProgress();
			var params = {
				id : id,
				jid : $api.getStorage('cfnetppjxid'),
				uid : $api.getStorage('cfnetppuid'),
			}
			cfnetppPOST(url_DiscussEdit, params, true, function(data, status) {
				api.hideProgress();
				if (status == 'success') {
					if (data.code == 200) {
						setQusetionData(data.data);
					}
				}
			})
		}

		//添加
		function setQusetionData(data) {
			//	      alert(JSON.stringify(data));
			data = data[0];
			$('#name').val(data.name);
			nameInitVal = $('#name').val();
			if (data.upfile != '' && data.upfile != null) {
				$('#img_div').show();
				$('#img_pic').attr('src', data.upfile);
				path = data.upfile;
				imgPathInitVal = data.upfile;//设置图片初始值
			}
			$('#content').text(data.content);
			contentInitVal = $('#content').val();//设置内容初始值
			chagetext();
			//			classarr = data.classlist;
			//			for (var i = 0; i < classarr.length; i++) {
			//				className = className + classarr[i].name + ",";
			//				classid = classid + classarr[i].id + ",";
			//			}
			//			className = className.slice(0, className.length - 1);
			//			classid = classid.slice(0, classid.length - 1);
			//			$("#select_class").text(className);
		}

		/**
		 *uid	创建者id	int	是
		 tid	互动类型id	int	是
		 jid	集团id	int	是
		 classid	班级id	array	是
		 name	互动名称	string	否
		 content	互动内容	text	否
		 file	图片路径	array	否 */
		function loaddata() {
			name = $('#name').val();
			if ($api.trimAll(name) == '') {
				ShowToast('请填写提问名称');
				return;
			}
			//			if (className == '') {
			//				ShowToast('请选择班级');
			//				return;
			//			}
			var content = $('#content').val();
			var url;
			if (type == 2) {//编辑
				url = url_EditActive;
				var params = {
					jid : $api.getStorage('cfnetppjxid'),
					uid : $api.getStorage('cfnetppuid'),
					id : id,
					//					classid : classid,
					name : name,
					content : content,
					file : path,
				}
			} else {//新增
				url = url_addActive;
				var params = {
					jid : $api.getStorage('cfnetppjxid'),
					uid : $api.getStorage('cfnetppuid'),
					tid : "2",
					classid : classid,
					name : name,
					content : content,
					file : path,
					cid : api.pageParam.cid,
					pid : api.pageParam.pid,
				}
			}
			//				alert(JSON.stringify(params));
			api.showProgress();
			cfnetppPOST(url, params, true, function(data, status) {
				if (type == 2) {//编辑提问
					SendBusinessOperationsLogNotifi('老师端提问编辑', '202', params, params, '', data, '老师端提问编辑接口', '202-01')
				} else {//创建提问
					SendBusinessOperationsLogNotifi('老师端提问创建', '203', params, params, '', data, '老师端提问创建接口', '203-01')
				}
				api.hideProgress();
				//								alert(JSON.stringify(data));
				if (status == 'success') {
					if (data.code == 200) {
						if (type == 2) {//编辑
							id = id;
						} else {
							id = data.iid;
						}
						$('#offlineASkAlert').show();
						api.sendEvent({
							name : 'refreshQusetionData'
						});
						//						api.closeWin({
						//						});
					} else if (data.code == 110) {
						ShowToast("提问名称不能重复");
					} else {
						ShowToast(data.message);
					}
				} else {
					ShowToast("网络请求失败");
				}
			})
		}

		//打开选择班级
		function openclass() {
			api.openWin({useWKWebView:true,
				name : 'select_class',
				url : 'select_class.html',
				animation : {
					type : "none", //动画类型（详见动画类型常量）
					subType : "from_right", //动画子类型（详见动画子类型常量）
					duration : 200
				},
				slidBackEnabled : false,
				pageParam : {
					data : classarr,
				},
			});
		}

		function keyback() {
			api.addEventListener({
				name : 'keyback'
			}, function(ret, err) {
				backClick();
			})
		}

		function actionDosign() {
			var params = {
				jid : $api.getStorage('cfnetppjxid'),
				uid : $api.getStorage('cfnetppuid'),
				iid : id,
				tid : 2,
				action : "start",
			}
			//				alert(JSON.stringify(params));
			cfnetppPOST(url_actionDosign, params, true, function(data, status) {
				//				alert(JSON.stringify(data));
				api.hideProgress();
				if (status == 'success') {
					if (data.code == 200) {
						$('#offlineASkAlert').hide();
						oningwin();
					}
				}
			})
		}

		//进行中
		function oningwin() {
			api.openWin({useWKWebView:true,
				name : 'TeacherQusertionNamesWind',
				url : 'TeacherQusertionNamesWind.html',
				pageParam : {
					name : name,
					id : id,
					type : 1,
					sourceurl : api.pageParam.sourceurl,
					sourcetitle : api.pageParam.sourcetitle
				},
			});
		}
	</script>
</html>